package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.common.dto.BasicDataByFind;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.pipe.mapper.dao.ClosureDao;
import com.deyuanyun.pic.pipe.service.ClosureService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 
 * @ClassName ClosureServiceImpl
 * @Description 管道基础数据 ->封堵物service实现
 * @author 荣华兴
 * @date 2015年8月31日
 */
@Service
public class ClosureServiceImpl implements ClosureService {

	@Resource
	private ClosureDao closureDao;
	
	/**
	 * @Description: 根据条件查询
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
	 * @Return:DTOResult
	 */
	@Override
	public Page<HashMap<String, Object>> findByCondition(BasicDataByFind bData) {
		Page<HashMap<String, Object>> list = closureDao
				.findByCondition(bData);
		Page<HashMap<String, Object>> list2 = new Page<HashMap<String, Object>>();
		HashMap<String, Object> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = list.get(i);
			String closureType = CodeUtil.getName(map.get("CLOSURETYPE")
					.toString());
			String closureMeterial = CodeUtil.getName(map
					.get("CLOSUREMATERIAL").toString());
			String antiseptiMeterial = CodeUtil.getName(map.get(
					"ANTISEPTICMATERIAL").toString());
			map.put("CLOSURETYPE", closureType);
			map.put("CLOSUREMATERIAL", closureMeterial);
			map.put("ANTISEPTICMATERIAL", antiseptiMeterial);
			list2.add(map);
		}
		list2.setPageNum(list.getPageNum());
		list2.setTotal(list.getTotal());
		list2.setPageSize(list.getPageSize());
		return list2;

	}

	/**
	 * @Description: 根据传入Id，查询对应Id的详细数据
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param id
	 * @参数：@return
	 * @Return:DTOResult
	 */
	@Override
	public DTOResult findById(String id) {
		List list = null;

		try {
			list = closureDao.findById(id);
		} catch (SQLException ex) {

			ex.printStackTrace();
		}
		return new DTOResult(ExceptionCode.SUCCESS.getCode(),
				ExceptionCode.SUCCESS.getLabel(), list);

	}

	/**
	 * @Description: 根据页面传入的值，查询导出值
	 * @Author: 荣华兴
	 * @Create Date: 2015年8月31日
	 * @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
	 * @Return:List<List<String[]>> 封堵物导出列表
	 */
	@Override
	public List<List<String[]>> excelExport(BasicDataByFind bData) {
		List<List<String[]>> result = new ArrayList<List<String[]>>();
		ArrayList<LinkedHashMap<String, Object>> list = new ArrayList<LinkedHashMap<String, Object>>();
		ArrayList<LinkedHashMap<String, Object>> list2 = new ArrayList<LinkedHashMap<String, Object>>();
		// 将数据取出放入list中
		list = closureDao.excelExport(bData);
		LinkedHashMap<String, Object> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = list.get(i);
			String closureType = CodeUtil.getName(map.get("CLOSURETYPE")
					.toString());
			String closureMeterial = CodeUtil.getName(map
					.get("CLOSUREMATERIAL").toString());
			String antiseptiMeterial = CodeUtil.getName(map.get(
					"ANTISEPTIC_MATERIAL").toString());
			map.put("CLOSURETYPE", closureType);
			map.put("CLOSUREMATERIAL", closureMeterial);
			map.put("ANTISEPTIC_MATERIAL", antiseptiMeterial);
			list2.add(map);
		}
		if (list2.size() == 0) {
			list2 = null;
		}
		result.add(JXL.toList(list2, "yyyy-MM-DD"));
		return result;
	}


 


}
